home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / ust1a.arc / EXPAND.C < prev    next >
Encoding:
C/C++ Source or Header  |  1985-09-05  |  3.8 KB  |  192 lines

  1.  
  2. /*+
  3.  * File: expand.c
  4.  * Description:
  5.  *  Expand mimics Unix's expand. It is not a full implementation.
  6.  *  Does not support [-tab1,tab2,..,tabn].
  7.  *
  8.  *  Expand is a filter.
  9.  *  It processes the named files or the standard input.
  10.  *  It writes to standard output with tabs changed into blanks.
  11.  *  
  12.  * Usages:
  13.  *  expand [-tabstop] [i_files]
  14.  *
  15.  *
  16.  *  Author: Mohsen Banan.
  17.  *
  18.  *  This program is public domain software, no warranty intended or
  19.  *  implied.
  20.  *  General permission to copy or modify, but not for profit,  is
  21.  *  hereby  granted.
  22.  *
  23.  * Functions:
  24.  *
  25.  *
  26.  * Audit Trail:  $Log:    expand.c,v $
  27.  * Revision 1.1  85/08/28  08:38:31  mohsen
  28.  * original posting to the net
  29.  * 
  30.  * Revision 1.1  85/08/14  13:44:13  mohsen
  31.  * Initial revision
  32.  * 
  33.  * 
  34.  *
  35. -*/
  36.  
  37. #ifdef RCS_VER
  38. static char *rcs = "$Header: expand.c,v 1.1 85/08/28 08:38:31 mohsen Exp $";
  39. #endif
  40.  
  41. /* #includes */
  42. #include "mbstd.h"
  43. #include <stdio.h>
  44. #include <ctype.h>
  45. #include <fcntl.h>
  46. #ifdef unix
  47. #include "msc3.h"
  48. #endif
  49.  
  50. /* #defines */
  51.  
  52. /* external variables */
  53.  
  54. /* referenced external function declarations */
  55.  
  56. /* internal function declarations */
  57. PUBLIC VOID expand();
  58. PUBLIC VOID tab_putc();
  59. PUBLIC VOID cant_open();
  60. STATIC VOID usage();
  61.  
  62. /* global variables */
  63.  
  64. /* static variables */
  65. STATIC CHAR * prog_name;
  66. STATIC INT tabstop;
  67. STATIC INT cpos;
  68.  
  69. INT
  70. main (argc, argv)
  71. INT argc;
  72. CHAR * argv[];
  73. {
  74.     expand(argc, argv);
  75. }
  76.     
  77.  
  78. /*<
  79.  * Function:expand
  80.  * Description:
  81.  *  Parses the command line and calls tab_putc.
  82.  *
  83.  * Returns:
  84.  *  VOID
  85.  * 
  86. >*/
  87. PUBLIC VOID 
  88. expand (argc,argv)
  89. INT argc;
  90. CHAR * argv[];
  91. {
  92.     FILE * i_fp;
  93.     FILE * o_fp;
  94.  
  95.     INT i;
  96.     INT c;
  97.  
  98.     i_fp = stdin;
  99.     o_fp = stdout;
  100.     tabstop = 8;
  101.     prog_name = argv[0];
  102.     for (i=1; i<argc; ++i) {
  103.         if (*argv[i] == '-') {
  104.             /* To handle concatenated switches */
  105.             INT j;
  106.             j=i;
  107.             while (*(++argv[j])) {
  108.                 if (isdigit(*(argv[j]))) {
  109.                     tabstop = atoi(argv[j]);
  110.                     break;
  111.                 }
  112. #if 0
  113.                 switch (*argv[j]) {
  114.                 case 'o':
  115.                 case 'O':
  116.                     if ( !(o_fp = fopen(argv[++i], "w")) ) {
  117.                         cant_open (prog_name, argv[i]);
  118.                         exit (1);
  119.                     }
  120.                     break;
  121.                 default:
  122.                     usage();
  123.                     exit(1);
  124.                 } /* switch (*argv[j]) */
  125. #endif
  126.             } /* while (*(++argv[j])) */
  127.         } /* if '-' */
  128.         else {
  129.             if (!(i_fp = fopen (argv[i], "r"))) {
  130.                 cant_open (prog_name, argv[i]);
  131.                 exit (1);
  132.             } 
  133.             setmode (fileno(i_fp), O_TEXT);
  134.             setmode (fileno(o_fp), O_TEXT);
  135.             cpos = 0;
  136.             while (( c = getc (i_fp)) != EOF) {
  137.                 tab_putc (c, o_fp);
  138.             }
  139.             fclose (i_fp);
  140.         }
  141.     } /* for i<argc */
  142.     fclose (o_fp);
  143.     exit (0);
  144. }
  145.  
  146.  
  147. /*<
  148.  * Function:tab_putc
  149.  * Description:
  150.  *  Converts tabs to the correct number of spaces (tabstop).
  151.  *  tab_putc is recursive.
  152.  *
  153.  * Returns:
  154.  *  VOID
  155.  *
  156. >*/
  157. PUBLIC VOID
  158. tab_putc (c, o_fp)
  159. INT  c;
  160. FILE * o_fp;
  161. {
  162.     if (c == '\n') {
  163.         cpos = 0;
  164.         putc (c, o_fp);
  165.     } else if ( c== '\t') {
  166.         do {
  167.             tab_putc(' ', o_fp);
  168.         } while (cpos % tabstop);
  169.     }
  170.     else {
  171.         cpos++;
  172.         putc (c, o_fp);
  173.     }
  174. }
  175.  
  176. PUBLIC VOID
  177. cant_open (prog_name,filename)
  178. CHAR * prog_name;
  179. CHAR * filename;
  180. {
  181.     fprintf (stderr, "%s :can not open %s \n", prog_name, filename);
  182.  
  183. #if 0
  184. STATIC VOID
  185. usage ()
  186. {
  187.     fprintf (stderr, "Usage: %s [-tabstop] [i_files]\n", prog_name);
  188. }
  189. #endif
  190.  
  191.